package com.lqs.sofp.servlet;

import com.lqs.sofp.service.UserService;
import com.lqs.sofp.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UserRegisterServlet", value = "/User/register")
public class UserRegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String gender = request.getParameter("gender");


        UserService userRegister = new UserServiceImpl();
        Boolean success = userRegister.register(username, password,phone,gender);



        if (success) {//注册成功
            response.sendRedirect(request.getContextPath());
        } else {
            //登录不成功重定向到登录页面。
            PrintWriter out = response.getWriter();
            //response.sendRedirect(request.getContextPath() + "/index.jsp");
            out.print("注册失败，请返回重新注册");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
